<?php
/**
-------------------------
GNU GPL COPYRIGHT NOTICES
-------------------------
This file is part of FlexicaCMS.

FlexicaCMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlexicaCMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlexicaCMS.  If not, see <http://www.gnu.org/licenses/>.*/

/**
 * $Id$
 *
 * @author FlexicaCMS team <contact@flexicacms.com>
 * @link http://www.flexicacms.com/
 * @copyright Copyright &copy; 2009-2010 Gia Han Online Solutions Ltd.
 * @license http://www.flexicacms.com/license.html
 */
  
 class FrontController extends FController
 {
    private $_contentType;//html,xml,json

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu=array();
    public $pageId;
    public $pageRevision;
    public $templateId;
    public $templateRevision;
    
    public function init(){
        parent::init();
        Yii::app()->theme = Yii::app()->cmsManager->CmsTheme;
        Yii::app()->layout = Settings::DEFAULT_LAYOUT;
        //TODO: add your custom initialization code here
    }
    
    public function filters() {
        return array(
            'configureRenderer - error',
            'accessControl',
        );
    }
    
    public function filterConfigureRenderer($filterChain){
        //Set front theme
        if (Yii::app()->theme == null)
            Yii::app()->theme = Settings::THEME;
        Yii::app()->layout = $this->layout = Settings::DEFAULT_LAYOUT;
        //use CPradoViewRenderer
        if (Yii::app()->core->FrontendRenderer){
            $rendererClass = Yii::app()->core->FrontendRenderer;
            Yii::app()->setComponent('viewRenderer', new $rendererClass);
        }

        $filterChain->run();
    }
    
    public function actionError(){
        Yii::app()->theme = Yii::app()->cmsManager->CmsTheme;
        parent::actionError();
    }    
    
    public function setContentType($type)
    {
        if (!in_array(strtolower($type), array('html','xml','json')))
            $type = 'html';
        $this->_contentType = $type;
    }
    
    public function getContentType()
    {
        if ($this->_contentType === null)
        {
            $this->_contentType = Yii::app()->request->getParam('content_type', 'html');
        }
        return $this->_contentType;
    }    
    
    public function cmsWidget($params)
    {
        if ($this->getModule() && $this->getModule()->getId() === 'Cms' && $this->getId() === 'default')
        {
            return;
        }
        
        //find page id
        $pageUrl = Yii::app()->request->url;
        $model = PageUrl::model()->findByAttributes(array('url'=>$pageUrl));
        if (is_object($model))
        {
            $this->pageId = $model->page_id;
        }
//        else
//            throw new CHttpException(404, 'Oups, Page not found.');
        
        $result = FSM::_run('Cms.pageWidget.renderHtml', $params);
        if (! $result->hasErrors())
        {
            $htmlDoc = $result->html;
            $html = $htmlDoc->saveXML($htmlDoc->getElementsByTagName('root')->item(0));
            $html = str_replace(array('<root>', '</root>'), '', $html);
            $html = str_replace(array('<com>', '</com>', '<com/>'), '', $html);
            echo $html;
        }
    }
    
    public function render($view,$data=null,$return=false)
    {
        $reuqestedPageUrl = Yii::app()->urlManager->RequestedPageUrl;
//        $reuqestedPageUrl = str_replace(array('<','>'), array('[',']'), $reuqestedPageUrl);echo get_class($this).' '.$reuqestedPageUrl;Yii::app()->end();
        if ($reuqestedPageUrl !== null)
        {
            $content = FSM::run('Cms.Page.renderPage', array('url' => $reuqestedPageUrl, 'content'=>$data))->html;
//            $content = $content->saveHTML();
//            $content = str_replace(array('<!DOCTYPE html>'), '', $content);
            $content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.$content;
            if ($return)
            {
                return $content;
            }
            else
            {
                echo $content;
            }
        }
        else
        {
            return parent::render($view,$data,$return);
        }
    }
}
?>